package com.ly.emp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.common.core.utils.BaseUtils;
import com.ly.emp.bean.dto.PluraOrgDTO;
import com.ly.emp.entity.EpmBaseSuitunitOrg;
import com.ly.emp.enums.HubSuitUnitOrgTypeEnum;
import com.ly.emp.mapper.EpmBaseSuitunitOrgMapper;
import com.ly.emp.service.IEpmBaseSuitunitOrgService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 绩效考核部门-impl
 *
 * @author luoyun
 * @Date: 2025-05-10
 */
@Service
public class EpmBaseSuitunitOrgServiceImpl extends ServiceImpl<EpmBaseSuitunitOrgMapper, EpmBaseSuitunitOrg> implements IEpmBaseSuitunitOrgService {

    @Override
    public R getId(String id) {
        EpmBaseSuitunitOrg entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getPage(EpmBaseSuitunitOrg entity, Page<EpmBaseSuitunitOrg> page) {
        IPage<EpmBaseSuitunitOrg> pageList = this.page(
                page,
                new LambdaQueryWrapper<EpmBaseSuitunitOrg>()
                        .like(StrUtil.isNotEmpty(entity.getOrgName()), EpmBaseSuitunitOrg::getOrgName, entity.getOrgName())
                        .and(StrUtil.isNotEmpty(entity.getParentOrgCode()),
                                wq -> wq.eq(EpmBaseSuitunitOrg::getOrgCode, entity.getParentOrgCode())
                                        .or().eq(EpmBaseSuitunitOrg::getParentOrgCode, entity.getParentOrgCode())
                        )
                        .orderByAsc(EpmBaseSuitunitOrg::getOrgCode)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(EpmBaseSuitunitOrg entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(EpmBaseSuitunitOrg entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<EpmBaseSuitunitOrg> dto) {
        List<EpmBaseSuitunitOrg> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public List<EpmBaseSuitunitOrg> getListByEntity(EpmBaseSuitunitOrg entity) {
        return this.list(
                new LambdaQueryWrapper<EpmBaseSuitunitOrg>()
                        .eq(StrUtil.isNotBlank(entity.getSuitUnit()), EpmBaseSuitunitOrg::getSuitUnit, entity.getSuitUnit())
                        .eq(StrUtil.isNotBlank(entity.getOrgCode()), EpmBaseSuitunitOrg::getOrgCode, entity.getOrgCode())
                        .eq(StrUtil.isNotBlank(entity.getPsnNum()), EpmBaseSuitunitOrg::getPsnNum, entity.getPsnNum())
                        .eq(StrUtil.isNotBlank(entity.getOrgType()), EpmBaseSuitunitOrg::getOrgType, entity.getOrgType())
                        .eq(StrUtil.isNotBlank(entity.getAnalysisType()), EpmBaseSuitunitOrg::getAnalysisType, entity.getAnalysisType())
                        .eq(StrUtil.isNotBlank(entity.getIsAssess()), EpmBaseSuitunitOrg::getIsAssess, entity.getIsAssess())
                        .eq(StrUtil.isNotBlank(entity.getPluraList()), EpmBaseSuitunitOrg::getPluraList, entity.getPluraList())
                        .eq(StrUtil.isNotBlank(entity.getParentOrgCode()), EpmBaseSuitunitOrg::getParentOrgCode, entity.getParentOrgCode())
                        .in(CollUtil.isNotEmpty(entity.getOrgList()), EpmBaseSuitunitOrg::getOrgCode, entity.getOrgList())
                        .in(CollUtil.isNotEmpty(entity.getAnalysisTypeList()), EpmBaseSuitunitOrg::getAnalysisType, entity.getAnalysisTypeList())
                        .orderByAsc(EpmBaseSuitunitOrg::getOrgCode)
                        .orderByAsc(EpmBaseSuitunitOrg::getDisplayOrder)
        );
    }

    @Override
    public List<EpmBaseSuitunitOrg> getPluraTeamList(PluraOrgDTO dto) {
        String type = dto.getType();
        Boolean typeFlag = dto.getTypeFlag();
        /**
         * 合并集约化处理
         * 1：集约化的，排除被集约化的
         * 2：未被集约化的，
         */
        String usercode = BaseUtils.getWorkNum();
        LambdaQueryWrapper<EpmBaseSuitunitOrg> queryWrapper = new LambdaQueryWrapper<>();
        //1-集约化的
        queryWrapper.and(temp ->
                temp.eq(EpmBaseSuitunitOrg::getIsAssess, "true")
                        .eq(EpmBaseSuitunitOrg::getIsIntensification, "true")
                        .eq(StrUtil.isNotBlank(dto.getSuitUnit()), EpmBaseSuitunitOrg::getSuitUnit, dto.getSuitUnit())
                        .in(CollUtil.isNotEmpty(dto.getOrgCodeList()), EpmBaseSuitunitOrg::getOrgCode, dto.getOrgCodeList())
                        .eq(EpmBaseSuitunitOrg::getIsIntensificationFlag, "2")
                        .eq(ObjectUtil.equals(typeFlag, true) && "1".equals(type), EpmBaseSuitunitOrg::getPsnNum, usercode)
                        .eq(ObjectUtil.equals(typeFlag, true) && !"1".equals(type), EpmBaseSuitunitOrg::getPluraList, usercode)
        );
        //2-未集约化的，取直接考核的二级班组
        queryWrapper.or(temp ->
                temp.eq(EpmBaseSuitunitOrg::getIsAssess, "true")
                        .eq(StrUtil.isNotBlank(dto.getSuitUnit()), EpmBaseSuitunitOrg::getSuitUnit, dto.getSuitUnit())
                        .in(CollUtil.isNotEmpty(dto.getOrgCodeList()), EpmBaseSuitunitOrg::getOrgCode, dto.getOrgCodeList())
                        .eq(EpmBaseSuitunitOrg::getOrgType, HubSuitUnitOrgTypeEnum.team2.getCode())
                        .eq(EpmBaseSuitunitOrg::getIsIntensificationFlag, "-1")
                        .eq(ObjectUtil.equals(typeFlag, true) && "1".equals(type), EpmBaseSuitunitOrg::getPsnNum, usercode)
                        .eq(ObjectUtil.equals(typeFlag, true) && !"1".equals(type), EpmBaseSuitunitOrg::getPluraList, usercode)
        );
        queryWrapper
                .orderByAsc(EpmBaseSuitunitOrg::getOrgCode)
                .orderByAsc(EpmBaseSuitunitOrg::getDisplayOrder);
        return this.list(queryWrapper);
    }
}
